<script setup>
import { ElMessage } from 'element-plus'
import { ref } from "vue";
import { userRegisterService ,userLoginService} from "@/api/user.js";
import { useRouter } from "vue-router";
import {useTokenStore} from "../stores/token"
const router=useRouter()
//控制注册与登录表单的显示， 默认显示注册
const isRegister = ref(false);
// 数据模型
const registerData = ref({
  username: "",
  password: "",
  rePassword: "",
});
//定义规则用于检测密码是否一致
const checkRePassword = (rule, value, callback) => {
  if (value !== registerData.value.password) {
    callback(new Error("两次密码不一致"));
  } else {
    callback();
  }
};
//表单的规则
const rules = ref({
  username: [
    { required: true, message: "请输入用户名", trigger: "blur" },
    {
      min: 5,
      max: 16,
      message: "用户名长度在 5 到 16 个字符",
      trigger: "blur",
    },
  ],
  password: [
    { required: true, message: "请输入密码", trigger: "blur" },
    { min: 5, max: 16, message: "密码长度在 5 到 16 个字符", trigger: "blur" },
  ],
  rePassword: [
    { validator: checkRePassword, trigger: "blur" }, //自定义校验规则
  ],
});
//注册接口
const register = async () => {
  let res = await userRegisterService(registerData.value);
  ElMessage.success("注册成功");
  resetForm()//注册完清除数据
  router.push('/')
  
};
const login = async () => {
  let res= await userLoginService(registerData.value);
  ElMessage.success("登录成功");
  router.push('/')
  resetForm()//登录完清除数据
  useTokenStore().setToken(res.data)
};
const resetForm = () => {
  registerData.value = {
    username: "",
    password: "",
    rePassword: "",
  };
};
</script>

<template>
  <div class="login-container">
    <div class="login-header">
      <h1>清泓智巡</h1>
    </div>
    <div class="login-content">
      <!-- 注册表单 -->
      <el-form
        ref="form"
        size="large"
        autocomplete="off"
        v-if="isRegister"
        :model="registerData"
        :rules="rules"
        class="login-form"
      >
        <el-form-item>
          <h2 class="form-title">注册</h2>
        </el-form-item>
        <el-form-item prop="username">
          <el-input
            :prefix-icon="User"
            placeholder="请输入用户名"
            v-model="registerData.username"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            :prefix-icon="Lock"
            type="password"
            placeholder="请输入密码"
            v-model="registerData.password"
          ></el-input>
        </el-form-item>
        <el-form-item prop="rePassword">
          <el-input
            :prefix-icon="Lock"
            type="password"
            placeholder="请再次输入密码"
            v-model="registerData.rePassword"
          ></el-input>
        </el-form-item>
        <!-- 注册按钮 -->
        <el-form-item>
          <el-button
            class="button"
            type="primary"
            auto-insert-space
            @click="register"
          >
            注册
          </el-button>
        </el-form-item>
        <el-form-item class="flex">
          <el-link type="info" :underline="false" @click="isRegister = false">
            ← 返回登录
          </el-link>
        </el-form-item>
      </el-form>
      <!-- 登录表单 -->
      <el-form ref="form" size="large" autocomplete="off" :rules="rules" :model="registerData" class="login-form" v-else>
        <el-form-item>
          <h2 class="form-title">登录</h2>
        </el-form-item>
        <el-form-item prop="username">
          <el-input
            :prefix-icon="User"
            placeholder="请输入用户名"
            v-model="registerData.username"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            name="password"
            :prefix-icon="Lock"
            type="password"
            placeholder="请输入密码"
            v-model="registerData.password"
          ></el-input>
        </el-form-item>
        <el-form-item class="flex">
          <div class="flex remember-forgot">
            <el-checkbox>记住我</el-checkbox>
            <el-link type="primary" :underline="false">忘记密码？</el-link>
          </div>
        </el-form-item>
        <!-- 登录按钮 -->
        <el-form-item>
          <el-button
            class="button"
            type="primary"
            auto-insert-space
            @click="login"
            >登录</el-button
          >
        </el-form-item>
        <el-form-item class="flex">
          <el-link type="info" :underline="false" @click="isRegister = true">
            没有账号？立即注册 →
          </el-link>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.login-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  padding: 20px;
}

.login-header {
  text-align: center;
  margin-bottom: 40px;
  
  h1 {
    font-size: 32px;
    color: #409EFF;
    font-weight: bold;
    letter-spacing: 2px;
    text-shadow: 1px 1px 3px rgba(0, 0, 0, 0.1);
  }
}

.login-content {
  width: 100%;
  max-width: 420px;
  background: #fff;
  padding: 40px;
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
}

.login-form {
  .form-title {
    text-align: center;
    color: #333;
    margin-bottom: 30px;
    font-size: 24px;
  }
  
  .el-input {
    border-radius: 6px;
  }
  
  .button {
    width: 100%;
    height: 48px;
    font-size: 16px;
    border-radius: 6px;
    margin-top: 10px;
  }
  
  .flex {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  
  .remember-forgot {
    width: 100%;
  }
  
  .el-link {
    font-size: 14px;
  }
}

@media (max-width: 480px) {
  .login-content {
    padding: 30px 20px;
  }
}
</style>